package com.example.sqliteperformancetest;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.database.Cursor;
import android.database.DatabaseUtils.InsertHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

import java.util.Random;

public class Main extends Activity {
	
	Thread threadInsert = null;
	
	DBHelper dbhelper;
	SQLiteDatabase db;
	
	String sql;
	
	Random rnd = new Random();
	
	int timestamp_select = -1;
	
	ArrayList<String> listLog = new ArrayList<String>();
	
	TextView txtLog;
	Handler handler = new Handler();
	
	long elapseTime;
	
	int dotcode_count=1000;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		txtLog = (TextView)findViewById(R.id.txtLog);
		
		dbhelper = new DBHelper(this);
		db = dbhelper.getWritableDatabase();
		
		
		sql = "DELETE FROM test;";
		db.execSQL(sql);
		
		threadInsert = new Thread(runInsert);
		threadInsert.start();
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	private Runnable runHandler = new Runnable(){
    	public void run() {
    		txtLog.append(elapseTime+"\r\n");
    	}
	};
	
	private Runnable runInsert = new Runnable(){
    	public void run() {
    		int i = 0;
    		
    		long startTime = System.currentTimeMillis();
    		
    		db.beginTransaction();
        	try{
        		while(i<dotcode_count){
        			double x = rnd.nextDouble();
        			double y = rnd.nextDouble();
        			
        			sql = "INSERT INTO test (x,y) VALUES("+x+","+y+");";
            		db.execSQL(sql);
            		++i;
            		//SystemClock.sleep(10);
        		}
        		db.setTransactionSuccessful();
        		db.endTransaction();
        	}catch(Exception e){
        		Log.d("neosarchizo", e.getMessage());
        	}
    		
        	elapseTime = System.currentTimeMillis() - startTime;
        	
        	//Log.d("neosarchizo", String.valueOf(elapseTime));
        	handler.post(runHandler);
        	
        	sql = "DELETE FROM test;";
    		db.execSQL(sql);
    		
        	startTime = System.currentTimeMillis();
        	
        	i=0;
        	while(i<dotcode_count){
    			double x = rnd.nextDouble();
    			double y = rnd.nextDouble();
    			
    			sql = "INSERT INTO test (x,y) VALUES("+x+","+y+");";
        		db.execSQL(sql);
        		++i;
        		//SystemClock.sleep(10);
    		}
        	
        	elapseTime = System.currentTimeMillis() - startTime;
        	
        	//Log.d("neosarchizo", String.valueOf(elapseTime));
        	handler.post(runHandler);
        	
        	db.beginTransaction();
        	InsertHelper ih = new InsertHelper(db, "TEST_DB_TABLE");
        	try{
        		while(i<dotcode_count){
        			double x = rnd.nextDouble();
        			double y = rnd.nextDouble();
        			
        			ih.prepareForInsert();
	           		 ih.bind(0, x);
	           		 ih.bind(1, y);
	           		 ih.execute();
            		++i;
            		//SystemClock.sleep(10);
        		}
        		db.setTransactionSuccessful();
        		db.endTransaction();
        	}catch(Exception e){
        		Log.d("neosarchizo", e.getMessage());
        	}
        	
        	elapseTime = System.currentTimeMillis() - startTime;
        	
        	//Log.d("neosarchizo", String.valueOf(elapseTime));
        	handler.post(runHandler);
        	
        	db.execSQL("PRAGMA cache_size=6000");
        	db.execSQL("PRAGMA synchronous=OFF"); 
        	db.execSQL("PRAGMA count_changes=OFF");

        	sql = "DELETE FROM test;";
    		db.execSQL(sql);
    		
        	startTime = System.currentTimeMillis();
        	
        	i=0;
        	while(i<dotcode_count){
    			double x = rnd.nextDouble();
    			double y = rnd.nextDouble();
    			
    			sql = "INSERT INTO test (x,y) VALUES("+x+","+y+");";
        		db.execSQL(sql);
        		++i;
        		//SystemClock.sleep(10);
    		}
        	
        	elapseTime = System.currentTimeMillis() - startTime;
        	
        	//Log.d("neosarchizo", String.valueOf(elapseTime));
        	handler.post(runHandler);
        	
        	sql = "DELETE FROM test;";
    		db.execSQL(sql);
    		
			startTime = System.currentTimeMillis();
    		
    		db.beginTransaction();
        	try{
        		while(i<dotcode_count){
        			double x = rnd.nextDouble();
        			double y = rnd.nextDouble();
        			
        			sql = "INSERT INTO test (x,y) VALUES("+x+","+y+");";
            		db.execSQL(sql);
            		++i;
            		//SystemClock.sleep(10);
        		}
        		db.setTransactionSuccessful();
        		db.endTransaction();
        	}catch(Exception e){
        		Log.d("neosarchizo", e.getMessage());
        	}
    		
        	elapseTime = System.currentTimeMillis() - startTime;
        	
        	//Log.d("neosarchizo", String.valueOf(elapseTime));
        	handler.post(runHandler);
    	}
	};
	
	
}
